﻿<Window x:Class="Pdf2epub.MainWindow"
    xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
    xmlns:ribbon="clr-namespace:System.Windows.Controls.Ribbon;assembly=System.Windows.Controls.Ribbon"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Pdf2epub="clr-namespace:Pdf2epub"
    xmlns:xcad="http://schemas.xceed.com/wpf/xaml/avalondock"
    xmlns:UI="clr-namespace:Pdf2epub.UI"
    mc:Ignorable="d"
    Title="pdf2epub"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    Closing="OnMainWindowClosing"
    Loaded="OnMainWindowLoaded"
    Icon="Resources/app.ico" WindowState="Maximized">

    <Window.CommandBindings>
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.OpenPdf" CanExecute="OpenPdfCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.Save" CanExecute="SaveCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.Quit" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.AnalyzeFonts" CanExecute="AnalyzeFontCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.TransformHtml" CanExecute="TransformHtmlCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.TransformEpub" CanExecute="TransformEpubCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.AssignReplacement" CanExecute="AssignReplacemenCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.OverrideStyle" CanExecute="OverrideStyleCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.OverrideHtmlTag" CanExecute="OverrideHtmlTagCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.JoinPreviousBlock" CanExecute="JoinBlockCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.UnJoinPreviousBlock" CanExecute="JoinBlockCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.SetChapter" CanExecute="SetChapterCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.ExtractSource" CanExecute="ExtractSourceCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.FindText" CanExecute="FindTextCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.GotoPage" CanExecute="GotoPageCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.InsertNewTextblockHere" CanExecute="ContentBlockOperationCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.InsertNewTableblockHere" CanExecute="ContentBlockOperationCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.DeleteThisContentBlock" CanExecute="ContentBlockOperationCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.RemoveMarkedBlocks" CanExecute="RemoveMarkedBlocksCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.JoinMarkedBlocks" CanExecute="JoinMarkedBlocksCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.RemoveDuplicateWhitespaceFromParagraphBlocks" CanExecute="BulkJobOnBlocksCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.DeleteEmptyContentBlock" CanExecute="BulkJobOnBlocksCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.EditStyles" CanExecute="EditStylesCanExecute" Executed="OnCommandExecuted" />
        <CommandBinding Command="Pdf2epub:Pdf2EpubCommands.OpenTableConfiguration" CanExecute="OpenTableConfigurationCanExecute" Executed="OnCommandExecuted" />
    </Window.CommandBindings>

    <xctk:BusyIndicator Name="busyIndicator" IsBusy="False" BusyContent="{Binding BusyContentTemplate, ElementName=busyIndicator}">
        <xctk:BusyIndicator.BusyContentTemplate>
            <DataTemplate>
                <StackPanel Margin="4" Width="350">
                    <TextBlock Text="{Binding MainText}" FontWeight="Bold" HorizontalAlignment="Center" />
                    <TextBlock Text="{Binding DetailText}" HorizontalAlignment="Center" />
                </StackPanel>
            </DataTemplate>
        </xctk:BusyIndicator.BusyContentTemplate>

        <DockPanel LastChildFill="True">

            <ribbon:Ribbon DockPanel.Dock="Top" IsDropDownOpen="True">

                <RibbonTab Header="Main actions">
                    <RibbonGroup Header="File">
                        <RibbonButton Label="Open" 
                                      Command="Pdf2epub:Pdf2EpubCommands.OpenPdf" 
                                      SmallImageSource="Resources/16/document-open-7.png"
                                      LargeImageSource="Resources/32/document-open-7.png" />
                        <RibbonButton Label="Save" 
                                      Command="Pdf2epub:Pdf2EpubCommands.Save" 
                                      SmallImageSource="Resources/16/document-save-5.png"
                                      LargeImageSource="Resources/32/document-save-5.png" />
                    </RibbonGroup>

                    <RibbonGroup Header="Operations">
                        <RibbonButton Label="Extract" 
                                      Command="Pdf2epub:Pdf2EpubCommands.ExtractSource" 
                                      SmallImageSource="Resources/16/archive-extract-2.png"
                                      LargeImageSource="Resources/32/archive-extract-2.png" />
                        <RibbonButton Label="Analyze Fonts" 
                                      Command="Pdf2epub:Pdf2EpubCommands.AnalyzeFonts" 
                                      SmallImageSource="Resources/16/application-x-font-ttf.png"
                                      LargeImageSource="Resources/32/application-x-font-ttf.png" />
                        <RibbonButton Label="Transform to HTML" 
                                      Command="Pdf2epub:Pdf2EpubCommands.TransformHtml" 
                                      SmallImageSource="Resources/16/text-html.png"
                                      LargeImageSource="Resources/32/text-html.png" />
                        <RibbonButton Label="Transform HTML to ePub" 
                                      Command="Pdf2epub:Pdf2EpubCommands.TransformEpub" 
                                      SmallImageSource="Resources/16/book_16.png"
                                      LargeImageSource="Resources/32/book_32.png" />
                    </RibbonGroup>

                    <RibbonGroup Header="Navigate">
                        <RibbonGroup Header="Find text">
                            <RibbonTextBox Name="searchTextBox">
                                <RibbonTextBox.InputBindings>
                                    <KeyBinding Command="Pdf2epub:Pdf2EpubCommands.FindText" Key="Enter" />
                                    <KeyBinding Command="Pdf2epub:Pdf2EpubCommands.FindText" Key="Return" />
                                </RibbonTextBox.InputBindings>
                            </RibbonTextBox>
                            <RibbonButton Label="Find text" 
                                          Command="Pdf2epub:Pdf2EpubCommands.FindText" 
                                          SmallImageSource="Resources/16/system-search-6.png"
                                          LargeImageSource="Resources/32/system-search-6.png" />
                        </RibbonGroup>

                        <RibbonGroup Header="Goto page">
                            <RibbonTextBox Name="gotoPageTextBox">
                                <RibbonTextBox.InputBindings>
                                    <KeyBinding Command="Pdf2epub:Pdf2EpubCommands.GotoPage" Key="Enter" />
                                    <KeyBinding Command="Pdf2epub:Pdf2EpubCommands.GotoPage" Key="Return" />
                                </RibbonTextBox.InputBindings>
                            </RibbonTextBox>
                            <RibbonButton Label="Goto page" 
                                          Command="Pdf2epub:Pdf2EpubCommands.GotoPage" 
                                          LargeImageSource="Resources/32/system-search-6.png"
                                          SmallImageSource="Resources/16/system-search-6.png" />
                        </RibbonGroup>
                    </RibbonGroup>

                    <RibbonGroup Header="Configuraion">
                        <RibbonButton Label="Edit Styles" 
                                      LargeImageSource="Resources/32/accessories-text-editor-4.png" 
                                      SmallImageSource="Resources/16/accessories-text-editor-4.png"  
                                      Command="Pdf2epub:Pdf2EpubCommands.EditStyles" />
                        <RibbonButton Label="Table Configuration" 
                                      LargeImageSource="Resources/32/insert-table.png" 
                                      SmallImageSource="Resources/16/insert-table.png" 
                                      Command="Pdf2epub:Pdf2EpubCommands.OpenTableConfiguration" />
                    </RibbonGroup>
                    
                    <RibbonGroup Header="Process content">
                        <RibbonButton Label="Join all block marked as 'Join'" LargeImageSource="Resources/32/insert-link-2.png" Command="Pdf2epub:Pdf2EpubCommands.JoinMarkedBlocks" />
                        <RibbonButton Label="Remove all block marked as 'Remove'" LargeImageSource="Resources/32/delete_table_row.png" Command="Pdf2epub:Pdf2EpubCommands.RemoveMarkedBlocks" />
                        <RibbonButton Label="Remove duplicate whitespace" LargeImageSource="Resources/32/edit-delete-shred.png" Command="Pdf2epub:Pdf2EpubCommands.RemoveDuplicateWhitespaceFromParagraphBlocks" />
                        <RibbonButton Label="Remove empty blocks" LargeImageSource="Resources/32/mail-delete.png" Command="Pdf2epub:Pdf2EpubCommands.DeleteEmptyContentBlock" />
                    </RibbonGroup>

                    <RibbonGroup>
                        <RibbonButton Label="Quit" Command="Pdf2epub:Pdf2EpubCommands.Quit" LargeImageSource="Resources/32/application-exit-5.png" />
                    </RibbonGroup>

                </RibbonTab>

                <RibbonTab Header="Closed windows">
                    <RibbonGroup x:Name="rgClosedWindows" />
                </RibbonTab>
            </ribbon:Ribbon>

            <xcad:DockingManager x:Name="dockingManager">
                <xcad:LayoutRoot>

                    <xcad:LayoutPanel Orientation="Horizontal">

                        <xcad:LayoutAnchorablePaneGroup DockWidth="100">

                            <xcad:LayoutAnchorablePane DockWidth="100">
                                <xcad:LayoutAnchorable Title="Project Settings" Hiding="OnHiding">
                                    <xctk:PropertyGrid Name="propertyGrid" ShowSearchBox="False" ShowSortOptions="False" ShowTitle="False" />
                                </xcad:LayoutAnchorable>
                            </xcad:LayoutAnchorablePane>
                        </xcad:LayoutAnchorablePaneGroup>

                        <xcad:LayoutAnchorablePaneGroup Orientation="Horizontal">
                            <xcad:LayoutAnchorablePaneGroup Orientation="Vertical">
                                <xcad:LayoutAnchorablePaneGroup Orientation="Horizontal">

                                    <xcad:LayoutAnchorablePane>
                                        <xcad:LayoutAnchorable Title="HTML Preview" Hiding="OnHiding" x:Name="anchorabePreviewBrowser">

                                            <WebBrowser Name="selectedContentBlockPreview" />
                                        </xcad:LayoutAnchorable>
                                    </xcad:LayoutAnchorablePane>

                                    <xcad:LayoutAnchorablePane>
                                        <xcad:LayoutAnchorable Title="Properties of content block" Hiding="OnHiding" x:Name="anchorabePropertyGridContentBlock">

                                            <xctk:PropertyGrid Name="pgContextBlock" ShowSearchBox="False" ShowSortOptions="False" ShowTitle="False" ShowSummary="False" />
                                        </xcad:LayoutAnchorable>
                                    </xcad:LayoutAnchorablePane>
                                </xcad:LayoutAnchorablePaneGroup>
                                <xcad:LayoutAnchorablePane>

                                    <xcad:LayoutAnchorable Title="Found PDF Content" Hiding="OnHiding">

                                        <ListView
                                                Name="lvContentBlocks"
                                                ItemsSource="{Binding PreviewSource}"
                                                SelectionChanged="OnPreviewSourceListSelectionChanged"
                                                ContextMenuOpening="OnContextMenuPreviewSourceOpening"
                                                HorizontalContentAlignment="Stretch"
                                                SelectionMode="Extended">

                                            <ListView.ContextMenu>
                                                <ContextMenu Name="cmPreviewSource">
                                                    <MenuItem Header="Assign Font-Type Replacement to all blocks with same font" Name="cmItemPreviewSourceAssignment" />
                                                    <MenuItem Header="Override Style" Name="cmOverrideAssignment" />
                                                    <MenuItem Header="Override HTML Tag" Name="cmOverrideHtmlTag" />
                                                    <MenuItem Header="Join with previous block" Command="Pdf2epub:Pdf2EpubCommands.JoinPreviousBlock" />
                                                    <MenuItem Header="Unjoin with previous block" Command="Pdf2epub:Pdf2EpubCommands.UnJoinPreviousBlock" />
                                                    <MenuItem Header="Set Chapter" Name="cmItemPreviewSourceSetChapter" Command="Pdf2epub:Pdf2EpubCommands.SetChapter" />
                                                    <MenuItem Header="Insert new text block here" Name="cmInsertNewTextblockHere" Command="Pdf2epub:Pdf2EpubCommands.InsertNewTextblockHere" />
                                                    <MenuItem Header="Insert new table block here" Name="cmInsertNewTableblockHere" Command="Pdf2epub:Pdf2EpubCommands.InsertNewTableblockHere" />
                                                    <MenuItem Header="Delete this block" Name="cmDeleteThisContentBlock" Command="Pdf2epub:Pdf2EpubCommands.DeleteThisContentBlock" />
                                                </ContextMenu>
                                            </ListView.ContextMenu>

                                            <ListView.ItemTemplate>
                                                <DataTemplate>
                                                    <Border BorderBrush="Gray" BorderThickness="1" Margin="2" Padding="2">
                                                        <StackPanel Orientation="Vertical">
                                                            <Grid Background="#FFE6E6E6" DockPanel.Dock="Top">
                                                                <Grid.ColumnDefinitions>
                                                                    <ColumnDefinition Width="*" />
                                                                    <ColumnDefinition Width="*" />
                                                                    <ColumnDefinition Width="*" />
                                                                    <ColumnDefinition Width="*" />
                                                                    <ColumnDefinition Width="*" />
                                                                    <ColumnDefinition Width="*" />
                                                                    <ColumnDefinition Width="*" />
                                                                    <ColumnDefinition Width="*" />
                                                                    <ColumnDefinition Width="*" />
                                                                    <ColumnDefinition Width="*" />
                                                                    <ColumnDefinition Width="*" />
                                                                    <ColumnDefinition Width="*" />
                                                                    <ColumnDefinition Width="*" />
                                                                    <ColumnDefinition Width="*" />
                                                                </Grid.ColumnDefinitions>

                                                                <TextBlock Grid.Column="2" Text="Page: " FontWeight="Bold" />
                                                                <TextBlock Grid.Column="3" Text="{Binding SourcePage}" Width="20" />

                                                                <TextBlock Grid.Column="0" Text="Chapter: " FontWeight="Bold" />
                                                                <TextBlock Grid.Column="1" Text="{Binding Chapter}" Width="200" />

                                                                <TextBlock Grid.Column="4" Text="Orig. Font: " FontWeight="Bold" />
                                                                <TextBlock Grid.Column="5" Text="{Binding Font}" Width="100" />

                                                                <TextBlock Grid.Column="6" Text="Style: " FontWeight="Bold" />
                                                                <TextBlock Grid.Column="7" Text="{Binding StyleName}"  Width="100" />

                                                                <TextBlock Grid.Column="8" Text="HTML Tag: " FontWeight="Bold" />
                                                                <TextBlock Grid.Column="9" Text="{Binding HtmlTag}" Width="40" />

                                                                <TextBlock Grid.Column="10" Text="Detected Style: " FontWeight="Bold" />
                                                                <TextBlock Grid.Column="11" Text="{Binding DefaultReplacement}"  Width="80" />

                                                                <TextBlock Grid.Column="12" Text="Join: " FontWeight="Bold" />
                                                                <TextBlock Grid.Column="13" Text="{Binding JoinWithPreviousBlock}"  Width="30" />
                                                            </Grid>

                                                            <TextBlock Text="{Binding Content}" FontSize="14" TextWrapping="WrapWithOverflow" />

                                                            <UI:SubblockRenderer ContentBlock="{Binding}" />
                                                        </StackPanel>
                                                    </Border>
                                                </DataTemplate>
                                            </ListView.ItemTemplate>
                                        </ListView>
                                    </xcad:LayoutAnchorable>
                                </xcad:LayoutAnchorablePane>
                            </xcad:LayoutAnchorablePaneGroup>
                        </xcad:LayoutAnchorablePaneGroup>

                        <xcad:LayoutAnchorablePaneGroup DockWidth="100">

                            <xcad:LayoutAnchorablePane DockWidth="100 ">

                                <xcad:LayoutAnchorable Title="Font Analysis" Hiding="OnHiding" x:Name="anchorableFontAnalysis">
                                    <ListView Name="FontOverview"
                                                  ItemsSource="{Binding FontDisplayAssignmentDatas}"
                                                  SelectionMode="Single"
                                                  ContextMenuOpening="OnContextMenuFontOverviewOpening"
                                                  MouseDoubleClick="OnFontOverviewDoubleClick"
                                                  GridViewColumnHeader.Click="OnFontOverviewColumnHeaderClicked">

                                        <ListView.ContextMenu>
                                            <ContextMenu Name="cmFontOverview">
                                                <MenuItem Header="Assign Replacement" Name="cmFontOverviewAssignment" />
                                            </ContextMenu>
                                        </ListView.ContextMenu>

                                        <ListView.View>
                                            <GridView>
                                                <GridViewColumn Width="240" Header="Font" DisplayMemberBinding="{Binding FontName}" />
                                                <GridViewColumn Width="120" Header="Occurrances" DisplayMemberBinding="{Binding Occurance}" />
                                                <GridViewColumn Width="120" Header="HTML Mapping" DisplayMemberBinding="{Binding HtmlReplacementStyle}" />
                                            </GridView>
                                        </ListView.View>
                                    </ListView>
                                </xcad:LayoutAnchorable>

                                <xcad:LayoutAnchorable Title="Character replacements" Hiding="OnHiding">
                                    <DataGrid 
                                        Name="dgReplacementCharacters" 
                                        ItemsSource="{Binding CharacterReplacements}" 
                                        AutoGenerateColumns="true" 
                                        AutoGeneratingColumn="GeneratingColumnHeadersForDataGrid" 
                                        AddingNewItem="OnNewTextReplacementAdded"/>
                                </xcad:LayoutAnchorable>
                            </xcad:LayoutAnchorablePane>
                        </xcad:LayoutAnchorablePaneGroup>
                    </xcad:LayoutPanel>
                </xcad:LayoutRoot>
            </xcad:DockingManager>
        </DockPanel>
    </xctk:BusyIndicator>
</Window>